package renda.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import renda.dao.AdminDAO;
import renda.dao.BaseDAO;
import renda.pojo.Admins;
import renda.pojo.Topics;
import renda.pojo.Users;

@Repository("adminDao")
public class AdminDAOImpl extends BaseDAO implements AdminDAO{


	@SuppressWarnings("unchecked")
	public Admins get(Admins admin) {
		List list = getTemplate().findByExample(admin);
		Admins admin1 = new Admins();
		if(list.size()>0){
			admin1 =  (Admins)list.get(0);
		}
		return admin1;
	}

	@SuppressWarnings("unchecked")
	public List<Users> getUsers(Users user) {
		String sql = "";
		String uname = user.getUname();
		String email = user.getEmail();
		String address = user.getWordaddress();
		if(uname!=null&&!uname.equals("")){
			if(email!=null&&!email.equals("")){
				if(address!=null&&!address.equals("")){
					sql = "from Users where uname like '%"+uname.trim()+"%' and email like '%"+email.trim()+"%' and wordaddress like '%"+address.trim()+"%' order by uid desc";	
				}else{
					sql = "from Users where uname like '%"+uname.trim()+"%' and email like '%"+email.trim()+"%' order by uid desc";	
				}	
			}else{
				if(address!=null&&!address.equals("")){
					sql = "from Users where uname like '%"+uname.trim()+"%' and wordaddress like '%"+address.trim()+"%' order by uid desc";	
				}else{
					sql = "from Users where uname like '%"+uname.trim()+"%' order by uid desc";	
				}	
			}
		}else{
			if(email!=null&&!email.equals("")){
				if(address!=null&&!address.equals("")){
					sql = "from Users where  email like '%"+email.trim()+"%' and wordaddress like '%"+address.trim()+"%' order by uid desc";	
				}else{
					sql = "from Users where  email like '%"+email.trim()+"%' order by uid desc";	
				}	
			}else{
				if(address!=null&&!address.equals("")){
					sql = "from Users where  wordaddress like '%"+address.trim()+"%' order by uid desc";	
				}
			}
		}
		if(sql.equals("")){
			sql = "from Users";
		}
		List<Users> list;
		if(user.getUid()!=null){
			list = getTemplate().find("from Users where uid="+user.getUid());
		}else{
			list = getTemplate().find(sql.toString());
		}
		if(list.size()>0){
			return list;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Topics> getTopics(final int page, final int size) {
		List<Topics> list;
		list = getTemplate().executeFind(new HibernateCallback() {	
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery("from Topics t order by t.tid desc");
				query.setFirstResult((page-1)*size);
				query.setMaxResults(size);
				List list2 = query.list();
				return list2;
			}
		});
		if(list.size()>0){
			for (Topics topics : list) {
				Users user = (Users)getTemplate().find("from Users where uid="+topics.getUid()).get(0);
				topics.setUname(user.getUname());
			}
			return list;
		}
		return null;
	}
}


